<?php
function mol($config){
    $link = @mysql_connect($config['host'],$config['user'],$config['pass']);
    if(mysql_errno()){
        exit('数据库连接错误：'.mysql_error());
    }
    mysql_select_db($config['db']);
    mysql_set_charset($config['char']);

}
//没有分页的查询
function selec($config,$table,$where=''){
    mol($config);
    
    $sql = "select * from {$table} {$where}";

    $res = mysql_query($sql);


    $arr = array();
    while($row = mysql_fetch_assoc($res)){
        $arr[]=$row;
    }

    mysql_free_result($res);
   
    mysql_close();
    return $arr ;
   
}

//有分页的查询封装
function sele($config,$table,$where='',$dic="id desc"){
    mol($config);
    $p = isset($_GET['p'])?$_GET['p']:1;
    $_SESSION['p']=$p;
    $pian = ($p-1)*$config['ye'];

    
    $sql = "select * from {$table} {$where} order by {$dic} limit {$pian},{$config['ye']}";

    $cou = "select count(*) as shu from {$table}";
    
    $rescou = mysql_query($cou);
    $rowcou = mysql_fetch_assoc($rescou);

    $fen = ceil($rowcou['shu']/$config['ye']);
    

   
    $xia = $p+1;
    $shang = $p-1;

    if($shang<1){
        $shang = 1;
    }
    if($xia>$fen){
        $xia = $fen;
    }

    $_SESSION['fen']=$fen;
    $_SESSION['shang']=$shang;
    $_SESSION['xia']=$xia;



    $res = mysql_query($sql);
    $arr = array();
    while($row = mysql_fetch_assoc($res)){
        $arr[]=$row;
    }

    mysql_free_result($res);
    mysql_free_result($rescou);
    mysql_close();
    return $arr ;
    return $rescou ;
    
}


function add($config,$table,$post=''){
   mol($config);
   if($post==''){
        $post=$_POST;
   }
  $jian = '`'.implode('`,`',array_keys($post)).'`';
  $value = "'".implode("','",$post)."'";

   $sql = "insert into {$table} ($jian) values ($value)";//5.准备SQL语句

   $res = mysql_query($sql);//6.发送sql语句
    return $res;
    mysql_close();

}


function dele($config,$teble,$id){

    mol($config);
    $sql = "delete from {$teble} where id={$id}";//5.准备SQL语句

    $res = mysql_query($sql);//6.发送sql语句
    
    mysql_close();
    return $res;
}

function update($config,$table,$where,$post=''){
    mol($config);
    if($post==''){
        $post=$_POST;
    }
    $change = '';
    foreach ($post as $k => $v) {
       $change .="`{$k}`='{$v}',";
       
    }
    // var_dump($change);
    $change = rtrim($change,",");
    $sql = "update {$table} set {$change} {$where}";//5.准备SQL语句
    // var_dump($sql) ;
    // die;
    $res = mysql_query($sql);//6.发送sql语句

    mysql_close();
    return $res;
    

}



//上传方法
function up($name, $dir = '/admin/zuopin/wenjian', $max_size =2000000, $allow_type = array('jpg', 'png', 'gif'))
{
    $file = $_FILES[$name];
    
    // var_dump($file);die;
    $ext  = trim(strchr($file['name'], '.'), '.');
    // var_dump($ext);die;
    //判断错误号
    if ($file['error'] != 0) {
        echo '上传错误: ';
        switch ($_FILES['myfile']['error']) {
            case 1:die('上传文件大小超出了PHP配置中的约定值：upload_max_filesize');
            case 2:die('上传文件大小超出了表单中的约定值：MAX_FILE_SIZE');
            case 3:die('文件只被部分上载');
            case 4:die('没有上传任何文件');
            case 6:die('找不到临时文件夹');
            case 7:die('文件写入失败');
            default:die('末知错误');
        }
    }

    //判断上传的文件大小
    if ($file['size'] > $max_size) {
        die('上传文件太大了我受不了！');
    }

    //判断上传的文件格式
    if (!in_array($ext, $allow_type)) {
        die('你当我瞎啊，请传正确的格式！');
    }

    //随机文件名
    $filename = date('YmdHis') . mt_rand(100, 999) . '.' . $ext;
    // echo $filename;die;
  // echo $file['tmp_name'];die;
    if (is_uploaded_file($file['tmp_name'])) {
        // if (!file_exists($dir)) {
        //     $res = mkdir($dir);
        // }
        //var_dump($_SERVER['DOCUMENT_ROOT']);
        if (move_uploaded_file($file['tmp_name'], $_SERVER['DOCUMENT_ROOT'].'/'.trim($dir, '/') . '/' . $filename)) {

            return  '/'.trim($dir, '/') . '/' . $filename;
        } else {
            echo '错误：'.$_SERVER['DOCUMENT_ROOT'].'/'.trim($dir, '/');
        }
    } else {
        die('非法上传');
    }
}


   

?>